package com.bj58.ecdata.dashboard.constant;


/**
 * 
 * 流量指标
 * @author zhaoxiang 2015-1-26
 * @since 1.0
 */
public enum NetFlowIndex {

	pv, uv, vppv, lide_pv, lide_uv, pvDIVuv, vppvDIVuv;

	private static final String PATTEN_LINE = "page_type='%s' AND business_name='%s' GROUP BY %s";
	private static final String PATTEN_LINE_PVVP_UV = "business_name='%s' AND (page_type='all' OR page_type='detail') GROUP BY %s";

	/**
	 * 获取某类别流量趋势查询条件
	 * 
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-1-27
	 */
	public String getLineConditon(EnumCate cate,String dateGroup) {
		switch (this) {
		case vppv:
			return String.format(PATTEN_LINE,EnumPageType.detail.getDBName(),cate.getDBName(), dateGroup);
		case vppvDIVuv:// 既需要详情页vppv 又需要全部uv
			return String.format(PATTEN_LINE_PVVP_UV, cate.getDBName(), dateGroup);
		case lide_pv:
			return String.format(PATTEN_LINE, EnumPageType.listanddetail.getDBName(),cate.getDBName(), dateGroup);
		case lide_uv:
			return String.format(PATTEN_LINE, EnumPageType.listanddetail.getDBName(),cate.getDBName(), dateGroup);
		default:// pv uv pvDIVuv 只需要所有页面类型
			return String.format(PATTEN_LINE, EnumPageType.total.getDBName(),cate.getDBName(), dateGroup);
		}

	}

	private static final String PATTEN_PLATFORM = "business_name='%s' AND page_type='%s' GROUP BY %s";
	private static final String PATTEN_PLATFORM_PVVP_UV = "business_name='%s' AND (page_type='all' OR page_type='detail') GROUP BY %s";


	/**
	 * 获取某分平台流量趋势查询条件
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-1-29
	 */
	public String getPlatformCon(EnumCate cate,String groupBy) {
		switch (this) {
		case vppv:
			return String.format(PATTEN_PLATFORM,cate.getDBName(), EnumPageType.detail.getDBName(), groupBy);
		case vppvDIVuv:// 既需要详情页算vppv 又需要所有算uv
			return String.format(PATTEN_PLATFORM_PVVP_UV, cate.getDBName(), groupBy);
		case lide_pv:
			return String.format(PATTEN_PLATFORM, cate.getDBName(), EnumPageType.listanddetail.getDBName(), groupBy);
		case lide_uv:
			return String.format(PATTEN_PLATFORM, cate.getDBName(), EnumPageType.listanddetail.getDBName(), groupBy);
		default:// pv uv pvDIVuv 只需要所有
			return String.format(PATTEN_PLATFORM, cate.getDBName(), EnumPageType.total.getDBName(), groupBy);
		}

	}	

	private static final String PATTEN_SOURCE = "business_name='%s' AND page_type='%s' AND platform='%s' and source1!='all' and source2='all' GROUP BY %s";
	private static final String PATTEN_SOURCE_VPPV_UV = "business_name='%s' AND platform='%s' AND (page_type='all' OR page_type='detail') and source1!='all' and source2='all'  GROUP BY %s";

	/**
	 * 获取流量来源查询条件
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-1-29
	 */
	public String getSourceCon(EnumCate cate,EnumPlatform platEnum,String groupBy) {
		switch (this) {
			case vppv:
				return String.format(PATTEN_SOURCE, cate.getDBName(), EnumPageType.detail.getDBName(),platEnum.getColName(), groupBy);
			case vppvDIVuv:// 既需要详情页算vppv 又需要所有算uv
				return String.format(PATTEN_SOURCE_VPPV_UV, cate.getDBName(),platEnum.getColName(), groupBy);
			case lide_pv:
				return String.format(PATTEN_SOURCE, cate.getDBName(), EnumPageType.listanddetail.getDBName(),platEnum.getColName(), groupBy);
			case lide_uv:
				return String.format(PATTEN_SOURCE, cate.getDBName(), EnumPageType.listanddetail.getDBName(),platEnum.getColName(), groupBy);
			default:// pv uv pvDIVuv 只需要所有
				return String.format(PATTEN_SOURCE,  cate.getDBName(), "all",platEnum.getColName(), groupBy);
		}

	}	

	
	/**
	 * 获取某group by 维度获取查询列
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-1-29
	 */
	public String getColumnByGroup(String groupBy) {

		switch (this) {
		case pv:
			return groupBy+",SUM(pv)/count(distinct stat_date) as pv";
		case uv:
			return groupBy+",SUM(uv)/count(distinct stat_date) as uv";
		case vppv:
			return groupBy+",SUM(pv)/count(distinct stat_date) as pv";
		case lide_pv:
			return groupBy+",SUM(pv)/count(distinct stat_date) as pv";
		case lide_uv:
			return groupBy+",SUM(uv)/count(distinct stat_date) as uv";
		case pvDIVuv:
			return groupBy+",SUM(pv)/SUM(uv) as pvDIVuv";
		case vppvDIVuv:
			return groupBy+",SUM(if(page_type='detail',pv,0))/sum(if(page_type='all',uv,0)) as vppvDIVuv";
//			return groupBy+",page_type,SUM(pv)/count(distinct stat_date) as pv,SUM(uv)/count(distinct stat_date) as uv";
		default:
			return "";
		}

	}
	
}
